var app = getApp();
var cardModel = require("../../model/cardModel.js")
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [],
    cardTypes: ['全部', '计次卡', '储值卡'],
    cardType: '卡片类型',
    isShowType: false,
    typeIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init();
  },
  init: function() {
    this.loadCardList()
  },
  //改变类型
  changeType(e) {
    var index = e.currentTarget.id
    var type = this.data.cardTypes[index]
    this.setData({
      cardType: type,
      typeIndex: index,
      isShowType: false
    })
    if (index == 0) {
      this.loadCardList()
    } else if (index == 1) {
      this.loadCardCounts()
    } else {
      this.loadCardValues()
    }
  },
  showType() {
    this.setData({
      isShowType: !this.data.isShowType
    })

  },
  //空方法,阻止冒泡
  space() {},
  //卡片详情
  navToInfo(e) {
    var index = e.currentTarget.id
    var card= this.data.cardList[index]
    wx.navigateTo({
      url: '../card_info/card_info?id='+card.id+"&type="+card.type,
    })
  },
  //加载所有卡
  loadCardList() {
    var that=this
    util.showLoading("加载中")
    cardModel.getCardList(res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg);
      }
      that.setData({
        cardList: res.data
      })
    })
  },
  //加载次卡
  loadCardCounts() {
    var that = this
    util.showLoading("加载中")
    cardModel.getCardCounts(res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg);
      }
      that.setData({
        cardList: res.data
      })
    })
  },
  //加载储值卡
  loadCardValues() {
    var that = this
    util.showLoading("加载中")
    cardModel.getCardValues(res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg);
      }
      that.setData({
        cardList: res.data
      })
    })
  }
})